﻿//
// Copyright (c) 2008-2010 Wouter Demuynck
//
// Permission is hereby granted, free of charge, to any person obtaining a copy of this
// software and associated documentation files (the "Software"), to deal in the Software
// without restriction, including without limitation the rights to use, copy, modify, merge,
// publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons
// to whom the Software is furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in all copies or
// substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED,
// INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR
// PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE
// FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
// ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
//

using System;
using Microsoft.Build.Framework;
using Microsoft.Build.Utilities;

namespace BuildTools.MSBuildTasks
{
    /// <summary>
    /// 
    /// </summary>
    public class GenerateVersion : Task
    {
        public override bool Execute()
        {
            BuildNumberType buildType = (BuildNumberType)Enum.Parse(typeof(BuildNumberType), BuildType, true);
            RevisionNumberType revisionType = (RevisionNumberType)Enum.Parse(typeof(RevisionNumberType), RevisionType, true);
            DateTime startingDate = DateTime.Parse(StartingDate);

            Version version = new Version(Major, Minor);
            if (string.IsNullOrWhiteSpace(this.VersionFile))
            {
                Log.LogMessage(MessageImportance.Low, "Creating version without using a version file.");
                version = VersionNumberGenerator.GenerateVersion(new Version(Major, Minor), buildType, revisionType, startingDate);
            }
            else
            {
                version = VersionNumberGenerator.GenerateVersion(VersionFile, buildType, revisionType, startingDate);
            }

            Version = version.ToString();
            Major = version.Major;
            Minor = version.Minor;
            Build = version.Build;
            Revision = version.Revision;
            Log.LogMessage("Version number generated: {0}", version);
            return true;
        }

        public string VersionFile
        {
            get;
            set;
        }

        [Output]
        public int Major
        {
            get;
            set;
        }

        [Output]
        public int Minor
        {
            get;
            set;
        }

        [Output]
        public int Build
        {
            get;
            private set;
        }

        [Output]
        public int Revision
        {
            get;
            private set;
        }

        [Required]
        public string BuildType
        {
            get;
            set;
        }

        [Required]
        public string RevisionType
        {
            get;
            set;
        }

        [Required]
        public string StartingDate
        {
            get;
            set;
        }

        [Output]
        public string Version
        {
            get;
            private set;
        }
    }
}
